package com.homework.utils;

import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;

import java.util.Date;
import java.util.Map;


public class JwtUtil {
    private static final String SECRET = "lxq123456";
    //时间4小时
    private static final Long EXPIRE_TIME = 1000 * 60 * 60 * 4L;

    public static String createToken(Map<String, Object> payload) {
        return JWT.create()
                .addPayloads(payload)
                .setKey(SECRET.getBytes())
                .setExpiresAt(new Date(System.currentTimeMillis() + EXPIRE_TIME))
                .sign();
    }

    public static Map<String, Object> parseToken(String token) {
        JWT jwt = JWTUtil.parseToken(token).setKey(SECRET.getBytes());
        if (jwt.verify()) {
            return jwt.getPayloads();
        } else {
            throw new RuntimeException("token 无效！");
        }
    }
}
